package com.gzxw.mogublog.sms.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @program: mogublog
 * @author: gzxw
 * @create: 2023-04-14 18:33
 **/
@Component
@Slf4j
public class SendMailUtils {

    @Value(value = "${spring.mail.username}")
    private String SENDER;

    @Resource
    private JavaMailSender mailSender;

    public void sendEmail(String subject, String receiver, String text) {

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject(subject);
            helper.setText(text,true);
            helper.setTo(receiver);
            helper.setFrom(SENDER);

            mailSender.send(mimeMessage);

        } catch (MessagingException e) {
            log.error(e.getMessage());
        }
    }
}
